//index.js
const shikuang= require('../../config').shikuang;
const getCurrentPagesApi = require('../../utils/common').getCurrentPagesApi;
const host = require('../../config').host;
//获取应用实例
const app = getApp()

Page({
  data: {
    content:'',
    id:'',
    showModal:false,
    session:false,
    navs:[{img: "../../images/p1_3.png",name: "市场d况"},
          {img: "../../images/p1_4.png",name: "海关数据"},
          {img: "../../images/p1_5.png",name: "订购数据"},
          {img: "../../images/p1_6.png",name: "定制数据"},
          {img: "../../images/p1_7.png",name: "新闻资讯"},
          {img: "../../images/p1_8.png",name: "木材交易"},
          {img: "../../images/p1_9.png",name: "English"},
          {img: "../../images/p2_1.png",name: "Русский"}]

  },
  preventTouchMove: function () {
      // this.hideModal();
    },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return {
        title: '国数源',
        path: getCurrentPagesApi()
      }
    }
    
  },
  shezhi: function(){
    wx.navigateTo({
      url: "./setup"
    })
  },
  gongzhonghao: function(){
    wx.navigateTo({
      url: "./weixin_gzh"
    })
  },
  showDialogBtn: function() {
      this.setData({
        showModal: true
      })

    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    
    /**
     * 隐藏模态对话框
     */
    hideModal: function () {
      this.setData({
        showModal: false
      });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel: function () {
      this.hideModal();
    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm: function () {
      this.hideModal();
    },
    //  onShow:function(e){
    //    this.onLoad();
    // },
  onLoad: function () {
    // console.log(3232);
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        app.globalData.code = res.code
      }
    })
    var that = this
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            withCredentials: true,
            // lang: "zh_TW",
            success: res => {
              // console.log(app.globalData.userInfo.avatarUrl);
              // this.globalData.userInfo = res.userInfo
              wx.request({
                url: host + 'api/tourist/weixin_login',
                data: {
                    encryptedData: res.encryptedData,
                    iv: res.iv,
                    code: app.globalData.code
                },
                header: {
                    'content-type': 'application/json'
                },
                success: res => {
                  console.log(res);
                      var data = res.data.data.huiyuan[0]
                      if(data.touxiang.indexOf("public")){
                        data.touxiang = host + data.touxiang
                      }
                      if(data.touxiang == ""){
                        data.touxiang = '../../images/p3_18.png'
                      }
                      if(!data.name){
                        data.name = "国数源小迷帝"
                      }
                      //从数据库获取用户信息
                      if(res.data.sta == 1){
                        that.setData({
                          session:true,
                          content: data.touxiang,
                          name: data.name,
                          id: data.id
                        });
                      }else{
                        wx.navigateTo({
                          url: "./user"
                        })
                      }
                    }
                });
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
              // console.log('app.globalData.userInfo.avatarUrl');
              // that.setData({
              //   session:true,
              //   content:app.globalData.userInfo.avatarUrl
              // });
            },
            fail: function(){
              wx.login({
                success: function(){
                  wx.navigateTo({
                    url: "./user"
                  })
                }
              })
            }
          });
        }else{
          try {
            const value = wx.getStorageSync("token")
            if (value) {
              // Do something with return value
              console.log(value);
              wx.request({
                url: host + 'api/tourist/dologin',
                data: {
                    token: value,
                },
                header: {
                    'content-type': 'application/json'
                },
                success: function(res) {
                      //从数据库获取用户信息
                      // that.queryUsreInfo();
                      console.log(res);
                      // console.log(res.data.data.huiyuan[0].type);
                      var data = res.data.data.huiyuan[0]
                      if(data.touxiang.indexOf("public")){
                        data.touxiang = host + data.touxiang
                      }
                      if(data.touxiang == ""){
                        data.touxiang = '../../images/p3_18.png'
                      }
                      if(!data.name){
                        data.name = "国数源小迷帝"
                      }
                        if(res.data.sta == 1){
                          that.setData({
                            session:true,
                            content: data.touxiang,
                            name: data.name,
                            id: data.id
                          });
                        }else{
                          wx.navigateTo({
                            url: "./user"
                          })
                        }
                    }
                });
            }
          } catch (e) {
            // Do something when catch error
            // console.log(value)
          }
        }
      }
    })
    
  },
  weixin_login: function(){
    this.setData({
      showModal: false
    });
    wx.navigateTo({
      url: "../user/weixin_login"
    })
  },
  gsy_login: function(){
    this.setData({
      showModal: false
    });
    wx.navigateTo({
      url: "../user/gsy_login"
    })
  },
  ready: function() {
    var that = this
    wx.request({
      url: shikuang,
      success: function(res){
        console.log(res.data)
        that.setData({
          content: res.data
        })
      },
      fail: function(data){
        console.log(data)
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
